package com.roc.steelend.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;

/**
 * jwt token令牌生成
 */
public class JWTUtil {

    private static final String  SING="&^()5432&(";


    public static String sendToken(HashMap<String,String> map){

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,3);

        JWTCreator.Builder builder = JWT.create();
        builder.withExpiresAt(instance.getTime());
        map.forEach(builder::withClaim);

        String sign = builder.sign(Algorithm.HMAC256(SING));
        return sign;
    }

    public static DecodedJWT getToken(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        return verify;
    }
}
